<?php
    /*
     * delete http cahce articles.
     *
    */
    
    ini_set('max_execution_time', 3600);
    error_reporting(E_ALL);
    require_once('../config/constant.php');
    require_once(DAEMONE_HOME . '/lib/lib_database.php');
    require_once(DAEMONE_HOME . '/lib/lib_html_reader.php');
	
	echo date('l jS \of F Y h:i:s A'), "<br/>/r/n";
	
	update_cache_file(1, 30);
	update_cache_file(0, 30);
	// update cache files
	function update_cache_file($is_file=1, $limit=30)
	{
		$http_cache_file = execute_query("select * from http_cache_file where is_file = $is_file limit $limit");	
		echo "select * from http_cache_file where is_file = $is_file limit 1";
		echo date('l jS \of F Y h:i:s A'), "<br/>/r/n";

		$has_exception = FALSE;
		
		while ($cache_file = mysql_fetch_assoc($http_cache_file)) 
		{
			try 
			{
				$http_cache_id[] = $cache_file['http_cache_id'];
				if($cache_file['update_action'] == 0)
				{
					echo 'updating file/folder ', BASE_URL,'/index.php', $cache_file['uri'], "<br/>/r/n";
					html_retrieve(BASE_URL.'/index.php'.$cache_file['uri']);
				}
				else 
				{
					echo 'deleting file/folder ', APPLICATION_HOME.$cache_file['path'], "<br/>/r/n";
					shell_exec('rm -f '.APPLICATION_HOME.$cache_file['path']);
				}
			}
			catch (Exception $e) 
			{
				echo $e;
				$has_exception = TRUE;
			}
		}
		if(isset($http_cache_id))
		{
			execute_query('delete from http_cache_file where http_cache_id in ('.implode(',',$http_cache_id).')');	
			echo 'deleting http cache ids', implode(',',$http_cache_id), "<br/>/r/n";
		}
	}
	
	echo date('l jS \of F Y h:i:s A'),"<br/>/r/n";

	function rrmdir($dir) 
	{
		if (is_dir($dir)) 
		{
		 $objects = scandir($dir);
		 foreach ($objects as $object) {
		   if ($object != "." && $object != "..") {
			 if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object);
		   }
		 }
		 reset($objects);
		 rmdir($dir);
		}
	} 
?>
